<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="专业名称" prop="thirdSpeName">
        <el-input
          v-model="queryParams.thirdSpeName"
          placeholder="请输入专业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专业代码" prop="thirdSpeCode">
        <el-input
          v-model="queryParams.thirdSpeCode"
          placeholder="请输入专业代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专业门类" prop="firstSpeName">
        <el-input
          v-model="queryParams.firstSpeName"
          placeholder="请输入专业门类"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专业类" prop="secSpeName">
        <el-input
          v-model="queryParams.secSpeName"
          placeholder="请输入专业类"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="specialtyType">
        <el-select v-model="queryParams.specialtyType" clearable size="small" placeholder="请选择本科专科" @change="handleQuery">
          <el-option
            v-for="item in education"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:CompassSpecialty:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:CompassSpecialty:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:CompassSpecialty:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <importData
          v-hasPermi="['system:CompassSpecialty:edit']"
          title="专业"
          action="CompassSpecialty"
        >
        </importData>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:CompassSpecialty:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CompassSpecialtyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--<el-table-column label="主键" align="center" prop="id"/>-->
      <el-table-column label="专业名称" align="center" prop="thirdSpeName">
        <template #default="{row}">
          <div>{{row.thirdSpeName}}</div>
          <div>{{row.thirdSpeCode}}</div>
        </template>
      </el-table-column>
      <el-table-column label="专业门类" align="center" prop="firstSpeCode">
        <template #default="{row}">
          <div>{{row.firstSpeName}}</div>
          <div>{{row.firstSpeCode}}</div>
        </template>
      </el-table-column>

      <el-table-column label="专业类" align="center" prop="secSpeCode">
        <template #default="{row}">
          <div>{{row.secSpeName}}</div>
          <div>{{row.secSpeCode}}</div>
        </template>
      </el-table-column>

      <el-table-column label="报考硕士专业" align="center" prop="masterSpecialty" show-overflow-tooltip/>
      <el-table-column label="类型" align="center" prop="specialtyType" show-overflow-tooltip/>
      <el-table-column label="开设课程" align="center" prop="classNames" show-overflow-tooltip/>
      <el-table-column label="就业率" align="center" prop="specialtyEmployRate" show-overflow-tooltip/>
      <el-table-column label="培养方向" align="center" prop="culturePath" show-overflow-tooltip/>
      <el-table-column label="专业描述" align="center" prop="specialtyDesc" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:CompassSpecialty:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:CompassSpecialty:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改专业信息表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="一级专业名称" prop="firstSpeName">
              <el-input v-model="form.firstSpeName" placeholder="请输入一级专业名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="一级专业代码" prop="firstSpeCode">
              <el-input v-model="form.firstSpeCode" placeholder="请输入一级专业代码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二级专业名称" prop="secSpeName">
              <el-input v-model="form.secSpeName" placeholder="请输入二级专业名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二级专业代码" prop="secSpeCode">
              <el-input v-model="form.secSpeCode" placeholder="请输入二级专业代码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="三级专业名称" prop="thirdSpeName">
              <el-input v-model="form.thirdSpeName" placeholder="请输入三级专业名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="三级专业代码" prop="thirdSpeCode">
              <el-input v-model="form.thirdSpeCode" placeholder="请输入三级专业代码"/>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="报考硕士专业" prop="masterSpecialty">
              <el-input v-model="form.masterSpecialty" placeholder="请输入报考硕士专业"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类型" prop="specialtyType">
              <el-select v-model="form.specialtyType" clearable size="small" placeholder="请选择本科专科">
                <el-option
                  v-for="item in education"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="开设课程" prop="classNames">
          <el-tag
            :key="tag"
            v-for="tag in dynamicTags"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)">
            {{tag}}
          </el-tag>
          <el-input
            class="input-new-tag"
            v-if="inputVisible"
            v-model="inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="handleInputConfirm"
            @blur="handleInputConfirm"
          >
          </el-input>
          <temlate v-else>
            <el-button class="button-new-tag" size="small" @click="showInput">+ 新增</el-button>
          </temlate>
          <!--<el-input v-model="form.classNames" :autosize="{minRows: 4}" type="textarea" placeholder="请输入开设课程"/>-->
        </el-form-item>
        <el-form-item label="就业率" prop="specialtyEmployRate">
          <el-input v-model="form.specialtyEmployRate" :autosize="{minRows: 4}" type="textarea" placeholder="请输入就业率"/>
        </el-form-item>
        <el-form-item label="培养方向" prop="culturePath">
          <el-input v-model="form.culturePath" :autosize="{minRows: 4}" type="textarea" placeholder="请输入培养方向"/>
        </el-form-item>
        <el-form-item label="专业描述" prop="specialtyDesc">
          <el-input v-model="form.specialtyDesc" :autosize="{minRows: 4}" type="textarea" placeholder="请输入专业描述"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCompassSpecialty,
    getCompassSpecialty,
    delCompassSpecialty,
    addCompassSpecialty,
    updateCompassSpecialty,
    exportCompassSpecialty
  } from "@/api/system/CompassSpecialty";
  import dictMixins from "@/views/mixins/dictMixins";
  export default {
    mixins: [dictMixins],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 专业信息表表格数据
        CompassSpecialtyList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          thirdSpeName: null,
          thirdSpeCode: null,
          specialtyType: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        education: [],
        dynamicTags: [],
        inputVisible: false,
        inputValue: ''
      };
    },
    async created() {
      this.getList();
      this.education = await this.getDictByTypeName('education')
    },
    methods: {
      handleClose(tag) {
        this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      },

      showInput() {
        this.inputVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },

      handleInputConfirm() {
        let inputValue = this.inputValue;
        if (inputValue) {
          this.dynamicTags.push(inputValue);
        }
        this.inputVisible = false;
        this.inputValue = '';
      },

      /** 查询专业信息表列表 */
      getList() {
        this.loading = true;
        listCompassSpecialty(this.queryParams).then(response => {
          this.CompassSpecialtyList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          thirdSpeName: null,
          secSpeCode: null,
          thirdSpeCode: null,
          firstSpeName: null,
          secSpeName: null,
          firstSpeCode: null,
          masterSpecialty: null,
          specialtyType: null,
          classNames: null,
          specialtyEmployRate: null,
          culturePath: null,
          specialtyDesc: null,
          updateTime: null,
          createTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加专业信息表";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getCompassSpecialty(id).then(response => {
          this.form = response.data;
          this.dynamicTags = response.data.classNames.replace(/^\s+|\s+$/g, '').split(" ")
          this.open = true;
          this.title = "修改专业信息表";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            var classNames = JSON.parse(JSON.stringify(this.dynamicTags))
            this.form.classNames = classNames.join(' ')
            if (this.form.id != null) {
              updateCompassSpecialty(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addCompassSpecialty(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除专业信息表编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delCompassSpecialty(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有专业信息表数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportCompassSpecialty(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>

<style lang="scss" scoped>
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
